package rsi.mesoscopic.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class NetworkModel {

	private List<Node> mNodes;
	
	private int mCurrentStep = -1;
	
	private Map<Node, CarSource> mMapSources = new HashMap<Node, NetworkModel.CarSource>();
	
	private List<CarSource> mListSources = new ArrayList<NetworkModel.CarSource>();
	
	public NetworkModel() {
		mNodes = new ArrayList<Node>();
	}

	public synchronized boolean addNode(Node e) {
		return mNodes.add(e);
	}

	public synchronized Node getNode(int index) {
		return mNodes.get(index);
	}
	
	public synchronized Node getNodeById(String id) {
		for(Node n : mNodes) {
			if(n.getId().equals(id)) {
				return n;
			}
		}
		return null;
	}	

	public synchronized Node removeNode(int index) {
		return mNodes.remove(index);
	}

	public synchronized int nodeCount() {
		return mNodes.size();
	}
	
	public synchronized void setStep(int step) {
		mCurrentStep = step;
		for(Node n : mNodes) {
			n.setStep(step);
		}
	}
	
	public synchronized int getCurrentStep() {
		return mCurrentStep;
	}
	
	public synchronized void nextStep() {
		setStep(mCurrentStep+1);
	}
	
	public synchronized void previousStep() {
		if(mCurrentStep == 1) {
			return;
		}
		setStep(mCurrentStep-1);
	}
	
	public void addCarSource(String strNode, float arrivalRate) {
		CarSource source = new CarSource();
		
		source.arrivalRate = arrivalRate;
		
		Node node = getNodeById(strNode);
		
		mMapSources.put(node, source);
		mListSources.add(source);
	}
	
	public Map<Node, CarSource> getSource() {
		return mMapSources;
	}
	
	public void addCarsourceAmount(int index, int amount){
		mListSources.get(index).amount.add(amount);
	}
	
	public boolean hasMoreSteps() {
		return mCurrentStep < mNodes.get(0).getStepCount()-1;
	}
	
	public class CarSource {
		public float arrivalRate;
		public List<Integer> amount = new ArrayList<Integer>();
	}
}

